#!/bin/sh
. /etc/sipman.conf
echo "Running distribution specific dependencies<HR>"

testReplace ()
{
  local SOURCED=$1
  local SOURCEF=$2
  local DESTD=$3
  local DESTF=$4
  local DESTEXT=$5
  if [ "$DESTEXT" = "" ]; then
    DESTEXT=org
  fi
# echo "SOURCEF=$SOURCEF, DESTD=$DESTD, DESTF=$DESTF."
  if [ ! -d "$DESTD" ]; then
    echo "Oops, no directory found for $DESTD, $DESTF not updating."
  elif [ ! -f "$SOURCED/$SOURCEF" ]; then
    echo "Oops, no source file $SOURCED/$SOURCEF found."
  elif [ -f "$DESTD/$DESTF.$DESTEXT" ]; then
    echo "File $DESTD/$DESTF already upgraded."
  else
    echo "Backupping $DESTD/$DESTF."
    mv -v $DESTD/$DESTF $DESTD/$DESTF.$DESTEXT
    if [ ! -f "$DESTD/$DESTF" ]; then
      echo "Replacing $DESTD/$DESTF."
      cp -v $SOURCED/$SOURCEF $DESTD/$DESTF
    fi
  fi
}

testMkLink ()
{
  SRC=$1
  DST=$2
  if [ ! -f $SRC ]; then
    echo "Oops, $SRC not found to link."
  elif [ -h $DST ]; then
    echo "Oke, $DST already linked."
  elif [ -f $DST ]; then
    echo "Oops, $DST found as a file."
  elif [ ! -h $DST ]; then
    echo "Creating link from $SRC to $DST."
    ln -s $SRC $DST
  fi
}


#echo "prog: $0"
BASEDIR=$SIM_BASEDIR/main/install/debian/4.0
testReplace $BASEDIR etc-default-snmpd /etc/default snmpd
testReplace $BASEDIR etc-snmp-snmpd.conf /etc/snmp snmpd.conf
/etc/init.d/snmpd restart
testReplace $BASEDIR etc-default-tftpd-hpa /etc/default tftpd-hpa
TEST=`grep tftpd /etc/inetd.conf`
if [ "$TEST" != "" ] && [ ! -f /tmp/inetd.conf ]; then
  echo "Removing the tftpd from the inetd.conf file."
  grep -v tftpd /etc/inetd.conf > /tmp/inetd.conf
  mv -v /tmp/inetd.conf /etc/inetd.conf
fi
/etc/init.d/tftpd-hpa stop
/etc/init.d/tftpd-hpa start
testReplace $BASEDIR etc-sudoers /etc sudoers 20081009
/etc/init.d/sudo restart
PROCTYPE=`uname -m`
echo "Replacing proccgi.$PROCTYPE"
# update the binary for the proccgi utility.
PROCCGI=$SIM_BASEDIR/main/sources/proccgi.$PROCTYPE
if [ -f "$PROCCGI" ]; then
  cp -v $PROCCGI $SIM_BASEDIR/main/bin/proccgi
else
  echo "No proccgi found for your version ($PROCCGI)."
fi
SIMCRON=$SIM_BASEDIR/shared/configs/crontab
if [ ! -f $SIMCRON ]; then
  cp $SIM_BASEDIR/main/configs/crontab.txt $SIMCRON
  chmod a+x $SIMCRON
  chown root:root $SIMCRON
fi
testMkLink $SIMCRON /etc/cron.d/sipman3
testReplace $BASEDIR etc-default-syslogd /etc/default syslogd
testReplace $BASEDIR etc-syslog.conf /etc syslog.conf
for FILE in 0 1 2 3 4 5 6 7
do
  touch /var/log/local$FILE.log
done
chmod a+r /var/log/*.log
/etc/init.d/sysklogd restart
